var menuSetting = {
    view: {selectedMulti: false,showLine: false},check: {enable: true},data: {simpleData: {enable: true}},edit: {enable: false},
    callback: {
    	onClick: menuTreeClick
	}
};
var deptSetting = {
	    view: {selectedMulti: false,showLine: false},check: {enable: true,chkboxType:{ "Y" : "", "N" : "" }},data: {simpleData: {enable: true}},edit: {enable: false},
	    callback: {
	    	onClick: deptTreeClick
		}
	};
var subjectSetting = {
		view: {selectedMulti: false,showLine: false},check: {enable: true,chkboxType:{ "Y" : "", "N" : "" }},data: {simpleData: {enable: true}},edit: {enable: false},
	    callback: {
	    	
		}
};
var menuNodes = [];
var deptNodes = [];
var subjectNodes = [];
var roleId = "";
var menuId = "";
var deptId = "";
function initMenuTree(){
	$.post("/system/menu/findAllJsonByRole.do",{"roleId":roleId},function (data){
		var json = eval(data.result);
		menuNodes = json;
		$.fn.zTree.init($("#menuTree"), menuSetting, menuNodes);
	});
}
function initDeptTree(){
	$.post("/system/role/findAllDeptJson.do",{},function (data){
		var json = eval(data.result);
		deptNodes = json;
		$.fn.zTree.init($("#deptTree"), deptSetting, deptNodes);
	});
}

$(function (){
	roleId = $("#roleId").val();
	initMenuTree();
	initDeptTree();
	btnClick();
});

function btnClick(){
	//绑定菜单
	$("#menuSave").click(function (){
		var tree = $.fn.zTree.getZTreeObj("menuTree");
		var nodes = tree.getCheckedNodes(true);
		var ckNodes = new Array("");
		for(var i = 0 ; i < nodes.length ; i++){
			ckNodes.push(nodes[i].id);
		}
		RM.ajax("/system/role/bindMenu.do",{"ckNodes":ckNodes,"id":roleId},function (data){
			RM.alert(data.message);
		});
	});
	//绑定按钮
	$("#btnSave").click(function (){
		var all = $("#btnBody").find("input[type='checkbox']");
		if(all.length > 0){
			var allBtnIds = new Array();
			all.each(function (i,v){
				allBtnIds.push($(v).val());
			});
			var ckBtnIds = new Array("");
			$("#btnBody :checked").each(function (i,v){
				ckBtnIds.push($(v).val());
			});
			RM.ajax("/system/role/bindBtns.do",{"id":roleId,"ckBtnIds":ckBtnIds,"allBtnIds":allBtnIds},function (data){
				RM.alert(data.message);
			});
		}
	});
	//绑定部门
	$("#deptSave").click(function (){
		if(!menuId.isBlank()){
			var deptTree = $.fn.zTree.getZTreeObj("deptTree");
			//var allNodes = deptTree.transformToArray(deptTree.getNodes());
			var ckNodes = deptTree.getCheckedNodes(true);
			var ckDepts = new Array("");
			for(var i = 0 ; i < ckNodes.length ; i++){
				ckDepts.push(ckNodes[i].id);
			}
			RM.ajax("/system/role/bindMenuDept.do",{"id":roleId,"menuId":menuId,"ckDepts":ckDepts},function (data){
				RM.alert(data.message);
			});
		}else{
			RM.alert("请选择一个菜单在进行该操作");
		}
	});
	//绑定栏目
	$("#subjectSave").click(function (){
		if(!menuId.isBlank()){
			var subjectTree = $.fn.zTree.getZTreeObj("subjectTree");
			var allNodes = subjectTree.transformToArray(subjectTree.getNodes());
			if(allNodes.length > 0){
				var ckSubs = new Array("");
				var ckNodes = subjectTree.getCheckedNodes(true);
				for(var i = 0 ; i < ckNodes.length ; i++){
					ckSubs.push(ckNodes[i].id);
				}
				RM.ajax("/system/role/bindMenuSubject.do",{"id":roleId,"menuId":menuId,"deptId":deptId,"ckSubs":ckSubs},function (data){
					RM.alert(data.message);
				});
			}
		}else{
			RM.alert("请选择一个菜单在进行该操作");
		}
	});
}

function menuTreeClick(event, treeId, treeNode) {
	menuId = treeNode.id;
    $.post("/system/role/menuChange.do",{"menuId":menuId,"roleId":roleId,"deptId":deptId},function (data){
    	$("#btnBody").html(data.btnHtml);
    	var deptTree = $.fn.zTree.getZTreeObj("deptTree");
    	deptTree.checkAllNodes(false);
    	for(var i = 0 ; i < data.deptIds.length ; i++){
    		var node = deptTree.getNodeByParam("id", data.deptIds[i]);
    		deptTree.checkNode(node, true);
    	}
    	if(!deptId.isBlank()){
    		var subjectTree = $.fn.zTree.getZTreeObj("subjectTree");
    		subjectTree.checkAllNodes(false);
    		for(var i = 0 ; i < data.subIds.length ; i++){
    			var node = subjectTree.getNodeByParam("id", data.subIds[i]);
    			subjectTree.checkNode(node, true);
    		}
    	}
	});
};

function deptTreeClick(event, treeId, treeNode){
	if(!menuId.isBlank()){
		deptId = treeNode.id;
		$.post("/system/role/deptChange.do",{"menuId":menuId,"deptId":deptId,"roleId":roleId},function (data){
			var json = eval(data.result);
			subjectNodes = json;
			$.fn.zTree.init($("#subjectTree"), subjectSetting, subjectNodes);
			var subjectTree = $.fn.zTree.getZTreeObj("subjectTree");
			subjectTree.checkAllNodes(false);
			for(var i = 0 ; i < data.subjectIds.length ; i++){
	    		var node = subjectTree.getNodeByParam("id", data.subjectIds[i]);
	    		subjectTree.checkNode(node, true);
	    	}
		});
	}else{
		RM.alert("请选择一个菜单在进行该操作");
	}
}